![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Function hit test statistics provide a useful sanity check. If you discover that one routine is being called far more often than you expected it to be, you should look at the code and figure out why. You should also examine routines that are never called. Either modify the code so they are called properly, expand your test cases so they exercise these routines, or remove them if they are unnecessary.
Self-TestProgram Bad15, shown in Figure 15.7, uses some relatively slow code to perform simple encryption. Enter some text in the left text box and a numeric value in the Key field. Then click the Encrypt button to make the program encrypt the text. Enter encrypted text in the right text box and the key that was used to encrypt the text in the Key field. Then click the Decrypt button to make the program decrypt the text. This program is reasonably responsive for moderate amounts of text. If you enter a lot of text, however, the program slows down considerably. If you enter 1000 characters, the program takes 6 or 7 seconds to encrypt or decrypt the text. It should be able to run much faster. The code for program Bad15 follows. Enter the code or download it from the books Web page at www.vb-helper.com/err.htm. Run it using the Visual Basic Code Profiler to see where the program spends most of its time. Then make the fewest changes possible to improve the programs performance. You should be able to make the program much faster with only a few changes.
Option Explicit
Encrypt the text by adding the key number
to each character's ASCII code.
Private Sub cmdEncrypt_Click()
txtCiphertext.Text =
MousePointer = vbHourglass
DoEvents
Crypt txtPlaintext, txtCiphertext, Int(txtKey.Text)
MousePointer = vbDefault
End Sub
Encrypt the text by subtracting the key number
from each character's ASCII code.
Private Sub cmdDecrypt_Click()
txtPlaintext.Text =
MousePointer = vbHourglass
DoEvents
Crypt txtCiphertext, txtPlaintext, -Int(txtKey.Text)
MousePointer = vbDefault
End Sub
Encrypt the input text.
Private Sub Crypt(ByVal txtIn As TextBox, ByVal txtOut As TextBox, _
ByVal key As Integer)
Const MIN_ASC = 32 ASCII code for space.
Const MAX_ASC = 126 ASCII code for ~.
Const NUM_ASC = MAX_ASC - MIN_ASC + 1
Dim i As Integer
Dim ch As String
Dim ch_num As String
txtOut.Text =
For i = 1 To Len(txtIn.Text)
ch = Mid$(txtIn.Text, i, 1)
ch_num = Asc(ch)
Translate only printable characters.
If ch_num >= MIN_ASC And ch_num <= MAX_ASC Then
ch_num = ch_num + key - MIN_ASC
ch_num = (ch_num Mod NUM_ASC)
If ch_num < 0 Then ch_num = ch_num + NUM_ASC
ch_num = ch_num + MIN_ASC
End If
ch = Chr$(ch_num)
txtOut.Text = txtOut.Text & ch
Next i
End Sub
SummaryUse the Visual Basic Code Profiler to make sure all of the code is executed by your tests. For any line that does not execute, fix the code, expand the tests, or remove the line. Also use VBCP to identify the routines that would benefit most from optimization. Wait until the code is working correctly before you search for these routines. Then optimize only those that will provide significant improvement to the applications overall performance. The following list of Bug Stoppers summarizes the tests you can perform using VBCP.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|